<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require '../util/Includes.php';
$_accion = (int) $_GET[ 'accion' ];
//$_GET[ 'accion' ] = 2;
//$_accion = $_GET[ 'accion' ];
//$_GET[ 'u' ] = "usu_admin";
//$_GET[ 'p' ] = "12345";
$sResponse = CommonWeb::traerCodRespJSON( CODE_FAILED );
switch ( $_accion ) {
    case ACC_LOGIN:
        $sUsername = $_GET[ 'u' ];
        $sPassword = $_GET[ 'p' ];
        $user = CommonWeb::login( $sUsername, $sPassword );
        if ( !is_null( $user ) ) {
            if ( !is_numeric( $user ) ) {
                $_SESSION[ "usuario_logeado" ] = $sUsername;
                switch ( $user->getUsuarioTipoBean()->getIdUsuarioTipo() ) {
                    case TIPO_USU_ADMIN:
                        $_SESSION[ "modulo" ] = "admin";
                        break;
                    case TIPO_USU_SUP:
                        $_SESSION[ "modulo" ] = "supervision";
                        break;
                    case TIPO_USU_VEND:
                        $_SESSION[ "modulo" ] = "venta";
                        break;
                    default :
                        $_SESSION = array();
                        session_destroy();
                        break;
                }
                $sResponse = CommonWeb::traerCodRespJSON( CODE_SUCCESS );
            } else {
                $sResponse = CommonWeb::traerCodRespJSON( $user ); //USER OR PASSWORD INCORRECTS
            }
        } else {
            $sResponse = CommonWeb::traerCodRespJSON( EC_USER_NOT_REGISTERED );
        }
        break;
    case ACC_LOGOUT:
        $_SESSION = array( );
        if ( session_destroy() )
            $sResponse = CODE_SUCCESS;
        else
            $sResponse = CODE_FAILED;
        break;
    case ACC_GET_USER_LOGED :

        if ( isset( $_SESSION[ "usuario_logeado" ] )
                && !is_null( $_SESSION[ "usuario_logeado" ] ) ) {
            $sUsername = $_SESSION[ "usuario_logeado" ];
            $usuario = UsuarioDAO::traerPorUsername( $sUsername );
            $sResponse = UsuarioBean::returnJSONString( $usuario );
        }
        break;
    default:
        break;
}

echo $sResponse;
?>
